package com.atguigu.yygh.hosp.controller;
import com.alibaba.excel.util.StringUtils;
import com.atguigu.yygh.common.util.MD5;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import com.baomidou.mybatisplus.extension.api.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;
@CrossOrigin
@Api(tags = "医院设置管理")
@RestController

@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {
//    注入service进行调用
    @Autowired
    private HospitalSetService hospitalSetService;

//    1,查询所有的信息
    @ApiOperation(value = "获取所有信息")
    @GetMapping("findAll")
    public Result findAllHospitalSet()
    {
        List<HospitalSet> list = hospitalSetService.list();
        Result<List<HospitalSet>> ok = Result.ok(list);
        return  ok;
    }
//    2.删除医院的设置
    @DeleteMapping("{id}")
    @ApiOperation(value = "逻辑删除医院信息")//swagger注解
    public Result removeHospSet(@PathVariable Long id)
    {
        boolean res = hospitalSetService.removeById(id);
        if(res)
        {
            return Result.ok();
        }else return Result.fail();
    }
//    3.条件查询带分页
    @PostMapping("findPage/{current}/{limit}")
    public  Result findPageHost(@PathVariable long current, @PathVariable long limit,
                               @RequestBody(required = false) HospitalQueryVo vo)
    {
//      创建page对象
        Page<HospitalSet> page = new Page<>(current,limit);
       String hosname= vo.getHosname();
       String hoscode = vo.getHoscode();
//       构建条件查询
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
       if(!StringUtils.isEmpty(hoscode))
           wrapper.eq("hoscode",vo.getHoscode());
        if(!StringUtils.isEmpty(hosname))
            wrapper.like("hosname",vo.getHosname());

        hospitalSetService.page(page, wrapper);
        return Result.ok(page);
    }
//    4.添加医院设置
        @PostMapping("saveHospitalSet")
        public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet)
        {
            hospitalSet.setStatus(1);
            Random random = new Random();
            hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+
                    random.nextInt(1000)));
            boolean save = hospitalSetService.save(hospitalSet);
            if(save) return Result.ok();
            else return Result.fail();
        }
//    5.根据id获取医院设置
    @GetMapping("getHospitalSet/{id}")
    public Result getHospitalSet(@PathVariable long id)
    {
        HospitalSet byId = hospitalSetService.getById(id);

        return Result.ok(byId);
    }
//    6.修改医院设置
    @PostMapping("updateHospitalSet")
    public  Result updateHospitalSet(@RequestBody HospitalSet hospitalSet)
    {
        boolean b = hospitalSetService.updateById(hospitalSet);
        if(b) return Result.ok();
        else  return Result.fail();
    }
//    7.批量删除医院设置
    @DeleteMapping("batchremove")
    public  Result batchRemoveById(@RequestBody List<Long> list)
    {
        boolean b = hospitalSetService.removeByIds(list);
        if(b) return Result.ok();
        else  return Result.fail();
    }
//    8.医院设置锁定与解锁
    @PutMapping("lockHospitalSet/{id}/{status}")
    public  Result lockHospitalSet(@PathVariable long id,@PathVariable int status)
    {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }
//    9.发送秘钥
    @PutMapping("sendkey/{id}")
    public  Result sendKey(@PathVariable long id)
    {
        HospitalSet hosp = hospitalSetService.getById(id);
        String hoscode = hosp.getHoscode();
        String signKey = hosp.getSignKey();
//        这里发送短信的功能暂时不写
        return  Result.ok();
    }
}
